package com.tencent.bugly.crashreport.crash;

import android.content.Context;
import com.tencent.bugly.crashreport.common.strategy.StrategyBean;
import com.tencent.bugly.proguard.am;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.ap;
import com.tencent.bugly.proguard.aq;
import java.util.LinkedHashMap;
import java.util.Map;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class d {
    private static d a;
    private com.tencent.bugly.crashreport.common.strategy.a b;
    private com.tencent.bugly.crashreport.common.info.a c;
    private b d;
    private Context e;

    private d(Context context) {
        c a2 = c.a();
        if (a2 == null) {
            return;
        }
        this.b = com.tencent.bugly.crashreport.common.strategy.a.a();
        this.c = com.tencent.bugly.crashreport.common.info.a.a(context);
        this.d = a2.p;
        this.e = context;
        am.a().a(new Runnable() { // from class: com.tencent.bugly.crashreport.crash.d.1
            @Override // java.lang.Runnable
            public void run() {
                d.this.b();
            }
        });
    }

    public static d a(Context context) {
        if (a == null) {
            a = new d(context);
        }
        return a;
    }

    public static void a(final Thread thread, final int i, final String str, final String str2, final String str3, final Map<String, String> map) {
        am.a().a(new Runnable() { // from class: com.tencent.bugly.crashreport.crash.d.2
            @Override // java.lang.Runnable
            public void run() {
                try {
                    if (d.a != null) {
                        d.a.c(thread, i, str, str2, str3, map);
                    } else {
                        an.e("[ExtraCrashManager] Extra crash manager has not been initialized.", new Object[0]);
                    }
                } catch (Throwable th) {
                    if (!an.b(th)) {
                        th.printStackTrace();
                    }
                    an.e("[ExtraCrashManager] Crash error %s %s %s", str, str2, str3);
                }
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void b() {
        an.c("[ExtraCrashManager] Trying to notify Bugly agents.", new Object[0]);
        try {
            Class<?> cls = Class.forName("com.tencent.bugly.proguard.o");
            this.c.getClass();
            aq.a(cls, "sdkPackageName", "com.tencent.bugly", (Object) null);
            an.c("[ExtraCrashManager] Bugly game agent has been notified.", new Object[0]);
        } catch (Throwable unused) {
            an.a("[ExtraCrashManager] no game agent", new Object[0]);
        }
    }

    private CrashDetailBean b(Thread thread, int i, String str, String str2, String str3, Map<String, String> map) {
        CrashDetailBean crashDetailBean = new CrashDetailBean();
        crashDetailBean.C = com.tencent.bugly.crashreport.common.info.b.i();
        crashDetailBean.D = com.tencent.bugly.crashreport.common.info.b.g();
        crashDetailBean.E = com.tencent.bugly.crashreport.common.info.b.k();
        crashDetailBean.F = this.c.p();
        crashDetailBean.G = this.c.o();
        crashDetailBean.H = this.c.q();
        crashDetailBean.w = aq.a(this.e, c.e, c.h);
        crashDetailBean.b = i;
        crashDetailBean.e = this.c.h();
        crashDetailBean.f = this.c.p;
        crashDetailBean.g = this.c.w();
        crashDetailBean.m = this.c.g();
        StringBuilder sb = new StringBuilder();
        String str4 = "";
        sb.append("");
        sb.append(str);
        crashDetailBean.n = sb.toString();
        crashDetailBean.o = "" + str2;
        if (str3 != null) {
            String[] split = str3.split("\n");
            if (split.length > 0) {
                str4 = split[0];
            }
        } else {
            str3 = "";
        }
        crashDetailBean.p = str4;
        crashDetailBean.q = str3;
        crashDetailBean.r = System.currentTimeMillis();
        crashDetailBean.u = aq.b(crashDetailBean.q.getBytes());
        crashDetailBean.z = aq.a(c.f, false);
        crashDetailBean.A = this.c.e;
        crashDetailBean.B = thread.getName() + "(" + thread.getId() + ")";
        crashDetailBean.I = this.c.y();
        crashDetailBean.h = this.c.v();
        crashDetailBean.N = this.c.a;
        crashDetailBean.O = this.c.a();
        crashDetailBean.Q = this.c.H();
        crashDetailBean.R = this.c.I();
        crashDetailBean.S = this.c.B();
        crashDetailBean.T = this.c.G();
        this.d.c(crashDetailBean);
        crashDetailBean.y = ap.a();
        if (crashDetailBean.P == null) {
            crashDetailBean.P = new LinkedHashMap();
        }
        if (map != null) {
            crashDetailBean.P.putAll(map);
        }
        return crashDetailBean;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void c(Thread thread, int i, String str, String str2, String str3, Map<String, String> map) {
        String str4;
        Thread currentThread = thread == null ? Thread.currentThread() : thread;
        if (i == 4) {
            str4 = "Unity";
        } else if (i == 5 || i == 6) {
            str4 = "Cocos";
        } else if (i != 8) {
            an.d("[ExtraCrashManager] Unknown extra crash type: %d", Integer.valueOf(i));
            return;
        } else {
            str4 = "H5";
        }
        String str5 = str4;
        an.e("[ExtraCrashManager] %s Crash Happen", str5);
        try {
            if (!this.b.b()) {
                an.d("[ExtraCrashManager] There is no remote strategy, but still store it.", new Object[0]);
            }
            StrategyBean c = this.b.c();
            if (!c.g && this.b.b()) {
                an.e("[ExtraCrashManager] Crash report was closed by remote , will not upload to Bugly , print local for helpful!", new Object[0]);
                String a2 = aq.a();
                String str6 = this.c.e;
                String name = currentThread.getName();
                b.a(str5, a2, str6, name, str + "\n" + str2 + "\n" + str3, null);
                an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
                return;
            }
            if (i != 4) {
                if (i == 5 || i == 6) {
                    if (!c.l) {
                        an.e("[ExtraCrashManager] %s report is disabled.", str5);
                        an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
                        return;
                    }
                } else if (i == 8 && !c.m) {
                    an.e("[ExtraCrashManager] %s report is disabled.", str5);
                    an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
                    return;
                }
            }
            CrashDetailBean b = b(currentThread, i == 8 ? 5 : i, str, str2, str3, map);
            if (b == null) {
                an.e("[ExtraCrashManager] Failed to package crash data.", new Object[0]);
                an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
                return;
            }
            String a3 = aq.a();
            String str7 = this.c.e;
            String name2 = currentThread.getName();
            b.a(str5, a3, str7, name2, str + "\n" + str2 + "\n" + str3, b);
            if (!this.d.a(b)) {
                this.d.a(b, 3000L, false);
            }
            an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
        } catch (Throwable th) {
            try {
                if (!an.a(th)) {
                    th.printStackTrace();
                }
                an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
            } catch (Throwable th2) {
                an.e("[ExtraCrashManager] Successfully handled.", new Object[0]);
                throw th2;
            }
        }
    }
}
